package stardust.product.platform.webc.support;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import stardust.product.platform.webc.WebcException;


public class StaticUrlUtils
{
  
  public static final char RESTFUL_PREFIX = '{';
  public static final String RESTFUL_PREFIX_STRING = String.valueOf(StaticUrlUtils.RESTFUL_PREFIX);
  
  public static final char RESTFUL_SUFFIX = '}';
  public static final String RESTFUL_SUFFIX_STRING = String.valueOf(StaticUrlUtils.RESTFUL_SUFFIX);
  
  public static void main(String[] args)
  {
    System.out.println(StaticUrlUtils.getRestParameterMap("/user/123/456", "/user/{id}/{name}"));
  }
  
  public static String getMatchRegex(String restUri)
  {
    List<String> splitUri = StaticUrlUtils.splitRestParameter(restUri);
    StringBuffer buffer = new StringBuffer();
    
    for (String i : splitUri)
    {
      if (StaticUrlUtils.isRestParameter(i))
      {
        buffer.append(".*");
      }
      else
      {
        buffer.append(i);
      }
    }
    
    return buffer.toString();
  }
  
  public static boolean isVaildRestParameter(String parameter)
  {
    if (parameter != null && parameter.startsWith(StaticUrlUtils.RESTFUL_PREFIX_STRING) && parameter.endsWith(StaticUrlUtils.RESTFUL_SUFFIX_STRING) && parameter.length() > 2)
    {
      return true;
    }
    
    return false;
  }

  public static boolean isRestParameter(String parameter)
  {
    return parameter.startsWith(StaticUrlUtils.RESTFUL_PREFIX_STRING) && parameter.endsWith(StaticUrlUtils.RESTFUL_SUFFIX_STRING);
  }
  
  public static List<String> splitRestParameter(String restUri)
  {
    List<String> splitUri = new ArrayList<String>();
    
    int offset = 0;
    int i, j;
    
    do
    {
      i = restUri.indexOf(StaticUrlUtils.RESTFUL_PREFIX, offset);
      
      if (i == -1 && offset != restUri.length())
      {
        splitUri.add(restUri.substring(offset));
      }
      else if (i != -1 && offset != i)
      {
        splitUri.add(restUri.substring(offset, i));
      }
      
      if (i != -1)
      {
        offset = i;
        j = restUri.indexOf(StaticUrlUtils.RESTFUL_SUFFIX, offset);
        
        if (j == -1 || restUri.substring(i + 1, j + 1).indexOf(StaticUrlUtils.RESTFUL_PREFIX) != -1)
        {
          throw new WebcException("RESTful URL[" + restUri + "]解析失败，可能的错误[" + restUri.substring(0, i + 1) + "<缺少'" + StaticUrlUtils.RESTFUL_SUFFIX + "'结束符>" + restUri.substring(i + 1) + "]。");
        }
        else
        {
          j++;
          splitUri.add(restUri.substring(i, j));
          offset = j;
        }
      }
    }
    while (i != -1);
    
    for (int k = 0; k < splitUri.size(); k++)
    {
      if (StaticUrlUtils.isRestParameter(splitUri.get(k)))
      {
        if (!StaticUrlUtils.isVaildRestParameter(splitUri.get(k)))
        {
          throw new WebcException("RESTful URL[" + restUri + "]解析失败，没有提供参数名称。");
        }
        else if (k < splitUri.size() - 1 && StaticUrlUtils.isRestParameter(splitUri.get(k + 1)))
        {
          throw new WebcException("RESTful URL[" + restUri + "]解析失败，已发现连续的参数。");
        }
        else
        {
          for (int h = 0; h < splitUri.size(); h++)
          {
            if (h != k && splitUri.get(h).equals(splitUri.get(k)))
            {
              throw new WebcException("RESTful URL[" + restUri + "]解析失败，已发现重复的参数。");
            }
          }
        }
      }
    }
    
    return splitUri;
  }
  
  public static Map<String, String> getRestParameterMap(String uri, String restUri)
  {
    Map<String, String> result = new HashMap<String, String>();
    List<String> uriPattern;
    
    String tmpString;
    int offset = 0;
    int t;
    
    uriPattern = StaticUrlUtils.splitRestParameter(restUri);
    
    for (int i = 0; i < uriPattern.size(); i++)
    {
      if (StaticUrlUtils.isRestParameter(uriPattern.get(i)))
      {
        if (i < uriPattern.size() - 1)
        {
          t = uri.indexOf(uriPattern.get(i + 1), offset);
          
          if (t == -1)
          {
            throw new WebcException("RESTful URL[" + uri + " -> " + restUri + "]解析失败，格式不匹配。");
          }
          else
          {
            tmpString = uri.substring(offset, t);
            offset += tmpString.length();
            result.put(uriPattern.get(i), tmpString);
          }
        }
        else
        {
          tmpString = uri.substring(offset);
          offset += tmpString.length();
          result.put(uriPattern.get(i), tmpString);
        }
      }
      else
      {
        if (i < uriPattern.size() - 1)
        {
          tmpString = uri.substring(offset, offset + uriPattern.get(i).length());
        }
        else
        {
          tmpString = uri.substring(offset);
        }
        
        offset += tmpString.length();
        
        if (!tmpString.equals(uriPattern.get(i)))
        {
          throw new WebcException("RESTful URL[" + uri + " -> " + restUri + "]解析失败，格式不匹配。");
        }
      }
    }
    
    return result;
  }

}
